package it.antonio.ilpizze.swing.resource;

import it.antonio.ilpizze.swing.SwingException;
import it.antonio.ilpizze.swing.util.Converter;
import it.antonio.ilpizze.swing.util.Converters;
import it.antonio.ilpizze.swing.util.TextLayout;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

import javax.swing.ImageIcon;

public class Resources {
	
	private Class<?> clazz;
	private ResourceBundle resources;
	private Converters converters;
	
	public Resources(Class<?> clazz, ResourceBundle resources, Converters converters) {
		super();
		this.clazz = clazz;
		this.resources = resources;
		this.converters = converters;
	}

	public <T> T get(String key, Class<? extends T> typeToConvert){
		
		Converter<T> c = converters.get(typeToConvert);
		
		if(c != null){
			String resource = resources.getString(key); 
			return c.toObject(resource);
		}
		
		throw new SwingException("Cannot convert the resource to type " + typeToConvert);
	}
	
	public String getString(String key){
		return get(key, String.class);
	}
	
	public Integer getInt(String key){
		return get(key, Integer.class);
	}
	public Long getLong(String key){
		return get(key, Long.class);
	}
	public Double getDouble(String key){
		return get(key, Double.class);
	}
	public String[] getArray(String key, String split){
		String value = resources.getString(key);
		return value.split(split);
	}
	public String[] getArray(String key){
		return getArray(key,",");
	}
	public List<String> getList(String key, String split){
		return Arrays.asList(getArray(key, split));
	}
	
	public List<String> getList(String key){
		return getList(key, ",");
	}
	
	public Set<String> keySet() {
		return resources.keySet();
	}
	
	public ImageIcon getImage(String image){
		Path path = Paths.get(clazz.getResource(image).getPath());
		try {
			byte[] bytes = Files.readAllBytes(path);
			return new ImageIcon(bytes);
		} catch (IOException e) {
			throw new SwingException(e);
		}
	}
	
	public ImageIcon getImageFromFile(File image){
		try {
			byte[] bytes = Files.readAllBytes(Paths.get(image.toURI()));
			return new ImageIcon(bytes);
		} catch (IOException e) {
			throw new SwingException(e);
		}
	}
	
	public TextLayout getTextLayout(){
		return getTextLayoutWithName(clazz.getSimpleName());
	}
	
	public TextLayout getTextLayoutWithName(String fileName){
		try {
			Path path = Paths.get(clazz.getResource(fileName + ".layout").toURI());
			String content = new String(Files.readAllBytes(path));
			return new TextLayout(content);
		} catch (IOException | URISyntaxException e) {
			throw new SwingException(e);
		}
		
	}
	
	public TextLayout getTextLayoutFromFile(File file){
		try {
			String content = new String(Files.readAllBytes(Paths.get(file.toURI())));
			return new TextLayout(content);
		} catch (IOException e) {
			throw new SwingException(e);
		}
	}
}
